/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with
    Amrita ITEWS if not then it can be viewed here:
    http://itews.amrita.ac.in/license.html.


    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/


    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews


    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa
    Vidyapeetham as part of the project titled,"Intelligent & Interactive
    Telematics using Emerging Wireless Technologies for Transport Systems
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment
    Council (TIFAC), India.
***/
#define CONFIG_SPACE_DEF_VALS 1

#include <typedefs.h>
#include <config_api.h>
#include <string_tbl_api.h>
#include <string.h>
#include <flash_lpc21xx.h>
/*
typedef struct tag_strtbl_entry
{
    int id;
    char *val;
}strtbl_entry_t;

#ifdef CONFIG_SPACE_TDU
static const strtbl_entry_t entries[] =
{
    {TDU_DEFAULT_MSG_IS_ENABLED, "1"},
    {TDU_DEFAULT_MSG, "Amrita Research Labs"},
    {TDU_DISPLAY_TIMEOUT, "2000"},
    {TDU_SERVICE_PORT, "3334"},
    {TDU_SERVICE_NAME, "tdu"}
};
#endif

#ifdef CONFIG_SPACE_IGPSU
static const strtbl_entry_t entries[] =
{
    {IGPSU_UDPMSG_COUNT, "3"},
    {IGPSU_UDPMSG_DELAY, "1"},
    {IGPSU_SHUTDOWN_DELAY, "90"},
    {IGPSU_RELAY_TURNON_DELAY, "5"},
    {DIR_PUBLISHER_IS_ENABLED, "0"},
    {IGPSU_POWER_THRESHOLD, "10500"}
};
#endif


#ifdef CONFIG_SPACE_SPODAPP
static const strtbl_entry_t entries[] =
{
    {AUTH_USERNAME1, "admin"},
    {AUTH_PASSWORD1, "password"}
};
#endif

#define NUM_ENTRIES (sizeof(entries)/sizeof(strtbl_entry_t))

*/

STATUS stbl_init    ()
{
    int i, result;
    unsigned char *val;
    flash_lpc21xx_init();
    result = flash_lpc21xx_has_sign();
    if(result < 0)
    {
        for(i=0;i<CONFIG_SPACE_VAL_COUNT;i++)
        {
            val = (unsigned char *)config_space_def_vals[i];
            flash_lpc21xx_write_block(i+1, val, (strlen((char *)val)+1));
        }
        flash_lpc21xx_write_sign_word();
    }
    return 0;
}

STATUS stbl_get(uint16 id, char *valbuf, int nvalbuf)
{
    /*int i;
    for(i=0;i<NUM_ENTRIES;i++)
    {
        if(entries[i].id == id)
        {
            strncpy(valbuf, entries[i].val, nvalbuf);
            return 0;
        }
    }
    return -EINVAL;*/
    return (int32)flash_lpc21xx_read_block(id+1, (unsigned char *)valbuf, nvalbuf);
    // id+1 because the first block is reserved for sign word
}

STATUS stbl_put ( uint16 id, const char *val )
{
    //we're writing the \0 as well
    return flash_lpc21xx_write_block(id+1, (unsigned char *)val, strlen(val)+1);
    // id+1 because the first block is reserved for sign word
}

STATUS stbl_close   ()
{
    return 0;
}
